define(function (require, exports, module){
	var angular = require('angular');
	
	    angular.module('equationServiceApp',[])
        .factory('equationService',['$http',function($http){
            function httpGet(url,data){
                url = '/api/index.php?r=' + url ;
                return $http.get(url,{params:data});
            }
            
            function getReportItemConfig() {
                return {
                    "yjkj": [],
                    "rgkj": {
                        "plus": ["dqrg", "dqzq"],
                        "minus": ["dqrgdqzq", "dqrgdqtf", "lsrgdqtf", "dqzhuanqdqtf", "dqzhiqdqtf"],
                    },
                    "qykj": {
                        "plus": ["dqqy"],
                        "minus": ["dqqydqtf", "lsqydqtf"]
                    }
                };
            }
            
            function getReportConfig() {
                // todo
                var url = 'config/report-config/get';
                var data = {};
                return httpGet(url, data);
            }
                
            return {
                /**
                 *
                 * @param type
                 * @returns {*}
                 */
                getEquation: function (type){
                    
                    return getReportConfig()
                        .then(function (res){
                            
                            var data = res.data.data;
                            if (angular.isArray(data.list)){
                                res.data.list = {};
                            }
                            data.list = angular.extend({
                                "yjkj": {},
                                "rgkj": {},
                                "qykj": {}
                            },data.list);
                            data.list['rgkj']['dqrg'] = 1; //当期认购默认为1
                            data.list['qykj']['dqqy'] = 1; //当期签约默认显示
                            var reportConfig = data;
                            var itemConfig = getReportItemConfig();
                            
                            var equa = "";
                            var equas = {};
                            angular.forEach(itemConfig,function (val,key){
                                if (val.plus && val.minus) {
                                    var equation;
                                    var plus = [];
                                    var minus = [];
                                    for (var i = 0; i < val.plus.length; i++) {
                                        var item = val.plus[i];
                                        if (reportConfig.list[key][item] == 1) {
                                            plus.push(reportConfig.config[item]);                                      
                                        }                               
                                    }
                                 
                                    for (var i = 0; i < val.minus.length; i++) {
                                        var item = val.minus[i];
                                        if (reportConfig.list[key][item] == 1) {
                                            minus.push(reportConfig.config[item]);                                      
                                        }                               
                                    }
                                    
                                    equation = [plus.join('+').split(" "),minus.join('-').split(" ")].join('-');
                                    equas[key + 'Equation'] = equation.replace(/-$/,'');
                                }
                            });

                            if(type === 'all'){
                                return equas;
                            }else{
                                if (reportConfig.list['yjkj'].rg && reportConfig.list['yjkj'].rg == 1) {
                                    equa = equas['rgkjEquation'];
                                } else if (reportConfig.list['yjkj'].qy && reportConfig.list['yjkj'].qy == 1) {
                                    equa = equas['qykjEquation'];
                                }
                            }
                            return equa;
                        });
                    
                }
            };
        }]);
})